package com.eastidea.live.controller.mobi;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.collections.map.LinkedMap;
import org.bson.types.ObjectId;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.eastidea.live.dao.AppDao;
import com.eastidea.live.dao.FormDao;
import com.eastidea.live.dao.FormDataDao;
import com.eastidea.live.dao.UserDao;
import com.eastidea.live.entity.AppApplication;
import com.eastidea.live.entity.AppUser;
import com.eastidea.live.entity.FrmDtData;
import com.eastidea.live.entity.FrmInfMultipleChoice;
import com.eastidea.live.entity.FrmInfMultipleOptions;
import com.eastidea.live.entity.FrmInfSingleLineText;
import com.eastidea.live.entity.FrmUiField;
import com.eastidea.live.entity.FrmUiForm;
import com.eastidea.live.field.multiplechoice.MultipleChoiceDao;
import com.eastidea.live.field.multiplechoice.MultipleChoiceOptionsDao;
import com.eastidea.live.field.multiplechoice.MultipleChoiceOptionsTo;
import com.eastidea.live.field.multiplechoice.MultipleChoiceOt;
import com.eastidea.live.field.multiplechoice.MultipleChoiceService;
import com.eastidea.live.field.multiplechoice.MultipleChoiceTo;
import com.eastidea.live.field.pagebreak.PageBreakOt;
import com.eastidea.live.field.pagebreak.PageBreakService;
import com.eastidea.live.field.pagebreak.PageBreakTo;
import com.eastidea.live.field.singleline.SingleLineTextDao;
import com.eastidea.live.field.singleline.SingleLineTextOt;
import com.eastidea.live.field.singleline.SingleLineTextService;
import com.eastidea.live.field.singleline.SingleLineTextTo;
import com.eastidea.live.ot.AppOt;
import com.eastidea.live.ot.FormOt;
import com.eastidea.live.service.FieldService;
import com.eastidea.live.service.FormService;
import com.eastidea.live.to.AppTo;
import com.eastidea.live.to.FieldTo;
import com.eastidea.live.to.FormTo;
import com.eastidea.live.util.DateTimeUtil;
import com.eastidea.live.util.QrUtil;
import com.eastidea.live.util.RequestUtil;
import com.eastidea.live.util.StringUtil;
import com.eastidea.live.util.Constant;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;

@Controller
@RequestMapping(value = Constant.API_REST_V1 + "/form/")
public class FormGuestController {

	@Resource
	AppDao appDao;

	@Resource
	FormDao formDao;
	@Resource
	FormDataDao dataDao;

	@Resource
	FormService formService;
	@Resource
	FieldService fieldService;
	
	@RequestMapping(value = "/{formId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> info(@PathVariable("formId") String id, 
			HttpServletRequest request, ModelMap modelMap)
			throws UnknownHostException {
		Map<String, Object> res = new HashMap<String, Object>();

		Mongo mongo = new Mongo();
		DB db = mongo.getDB("palmio");
		boolean auth = db.authenticate("admin", "P2ssw0rd".toCharArray());
		System.out.println("auth: "+auth);
		DBCollection forms = db.getCollection("form");
		DBObject searchById = new BasicDBObject("_id", new ObjectId(id));
		DBObject formPo = forms.findOne(searchById);
		
		formPo.put("id", ((ObjectId) formPo.get("_id")).toString());
		
		res.put("frm", formPo);
		res.put("success", true);
		return res;
	}

	@RequestMapping(value = "submit", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> submit(HttpServletRequest request) throws UnknownHostException {
		Map<String, Object> res = new HashMap<String, Object>();

		String remoteIp;
		if (request.getHeader("x-forwarded-for") == null) {
			remoteIp = request.getRemoteAddr();
		} else {
			remoteIp = request.getHeader("x-forwarded-for");
		}
		
		Mongo mongo = new Mongo();
		DB db = mongo.getDB("palmio");
		boolean auth = db.authenticate("admin", "P2ssw0rd".toCharArray());
		System.out.println("auth: "+auth);
		DBCollection data = db.getCollection("data");

		String formId = request.getParameter("formId");

		Map pMap = request.getParameterMap();
		BasicDBObject form = new BasicDBObject();
		BasicDBList values = new BasicDBList();
		
		Date sData = new Date();
		for (Object name : pMap.keySet()) {			
			String nm = name.toString();
			if (nm.indexOf("@") > -1) {
				BasicDBObject value = new BasicDBObject();
				
				String[] arr = name.toString().split("@");
				String type = arr[0];
				String pname = arr[1];
				String indexx = arr[2];
				String uid = arr[3];
				String val = request.getParameter(nm);
				
				value.put("type", type);
				value.put("name", pname);
				value.put("value", val);				
				value.put("uid", uid);
				value.put("indexx", indexx);	
				
				values.add(value);
			}
		}
		
		form.put("formId", formId);
		form.put("submitTime", sData);
		form.put("ipAddr", remoteIp);
		form.put("values", values);
		data.insert(form);
		
		res.put("success", true);
		return res;
	}
	
	@RequestMapping(value = "rate", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> rate(HttpServletRequest request) throws UnknownHostException {
		Map<String, Object> res = new HashMap<String, Object>();

		String remoteIp;
		if (request.getHeader("x-forwarded-for") == null) {
			remoteIp = request.getRemoteAddr();
		} else {
			remoteIp = request.getHeader("x-forwarded-for");
		}
		
		Mongo mongo = new Mongo();
		DB db = mongo.getDB("palmio");
		boolean auth = db.authenticate("admin", "P2ssw0rd".toCharArray());
		System.out.println("auth: "+auth);
		DBCollection data = db.getCollection("data");

		String formId = request.getParameter("formId");
		String key = request.getParameter("formKey");
		String rate = request.getParameter("rate");

		BasicDBObject form = new BasicDBObject();
		BasicDBList values = new BasicDBList();
		
		Date sData = new Date();

		BasicDBObject value = new BasicDBObject();
		
		String[] arr = key.toString().split("@");
		String type = arr[0];
		String pname = arr[1];
		String indexx = arr[2];
		String uid = arr[3];
		
		value.put("type", type);
		value.put("name", pname);
		value.put("value", rate);
		value.put("uid", uid);
		value.put("indexx", indexx);	
		
		values.add(value);
		
		form.put("formId", formId);
		form.put("submitTime", sData);
		form.put("ipAddr", remoteIp);
		form.put("values", values);
		data.insert(form);
		
		res.put("success", true);
		return res;
	}
}
